/**
 *
 * 版权所有 © cybersky.com
 *
 */
package cn.cybertron.primus.report.data;

/**
 * 默认查询范围实现，支持绝对时间和相对时间
 */
public class DefaultQueryRange extends QueryRange.Range implements QueryRange {
	
	private static final long serialVersionUID = 5279673063628836937L;

	public static final long DAY = 86400000l;
	
	public static final long MINUTE = 60000l;
	
	public static final long HOUR = 3600000l;
	
	public static enum Type {
		relative, absolute
	};
	
	public static enum LastUnit {
		MINUTE, HOUR, DAY 
	}
	
	public DefaultQueryRange(long start, long stop) {
		super(start, stop);
		this.type = Type.absolute;
	}
	
	public DefaultQueryRange() {
		super();
	}

	protected Type type = Type.relative;
	
	protected int last = 1;
	
	/**
	 * 用来指定报表时间长度单位，默认为天。
	 */
	protected LastUnit lastUnit = LastUnit.DAY;
	
	@Override
	public Range getRange() {
		switch(lastUnit) {
			case MINUTE:
				return Type.absolute == type ? this : new Range(System.currentTimeMillis() - last * MINUTE, System.currentTimeMillis());
			case HOUR:
				return Type.absolute == type ? this : new Range(System.currentTimeMillis() - last * HOUR, System.currentTimeMillis());
			default:
				return Type.absolute == type ? this : new Range(System.currentTimeMillis() - last * DAY, System.currentTimeMillis()); 	
		}
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public int getLast() {
		return last;
	}

	public void setLast(int last) {
		//去除1个月的限制
//		if (last > 31) {
//			throw new IllegalArgumentException("last days must less than 31(one month)!");
//		}
		this.last = last;
	}

	public LastUnit getLastUnit() {
		return lastUnit;
	}

	public void setLastUnit(LastUnit lastUnit) {
		this.lastUnit = lastUnit;
	}
}
